// STARS

package demo1;

import java.awt.Color;
import java.awt.Graphics;

public class Star implements Comparable<Star> {
    // Constructor 1.
    public Star() {
        x = 0.0;    y = 0.0;    z = 0.0;
        px = 0.0;   py = 0.0;
        sx = 0;     sy = 0;
    }
    
    // Contructor 2 - Include position info.
    public Star(double X, double Y, double Z) {
        x = X;      y = Y;      z = Z;
        px = 0.0;   py = 0.0;
        sx = 0;     sy = 0;
    }
    
    // Return the Z position of the star.
    public double getZ() {
        return z;
    }
    
    // Set the position of the star.
    public void setPosition(double X, double Y, double Z) {
        x = X;      y = Y;      z = Z;
    }
    
    // Move the star.
    public void move(double X, double Y, double Z) {
        x += X;
        y += Y;
        z += Z;
    }
    
    // Project the star into 2D space. (Viewspace)
    public void project(double fovX, double fovY) {
        px = x / (z / fovX);
        py = y / (z / fovY);
    }
    
    // Map the star to screen space.
    public void map(int screenW, int screenH) {
        sx = (int)(px * (double)screenW / 2.0 + (double)screenW / 2.0);
        sy = (int)(-py * (double)screenH / 2.0 + (double)screenH / 2.0);
    }
    
    // Draw the star to the screen.
    public void draw(Graphics g, int screenW, int screenH, double fovX, double fovY, double Depth) {
        this.project(fovX, fovY);
        if(px >= -1.0 && px <= 1.0 && py >= -1.0 && py <= 1.0) {
            this.map(screenW, screenH);
            if(z <= Depth*0.3) {
                g.setColor(Color.white);
                g.fillOval(sx-3, sy-3, 7, 7);
            }
            if(z > Depth*0.3 && z <= Depth*0.6) {
                g.setColor(Color.gray);
                g.fillOval(sx-2, sy-2, 5, 5);
            }
            if(z > Depth*0.6) {
                g.setColor(Color.darkGray);
                g.fillOval(sx, sy, 3, 3);
            }
        }
    }
    
    // 3D position.
    private double x;
    private double y;
    private double z;
    
    // Viewspace position. (Projected to 2D)
    private double px;
    private double py;
    
    // Screenspace position.
    private int sx;
    private int sy;

    @Override
    public int compareTo(Star t) {
        int result = 0;
        try {
            if(this.getZ() > t.getZ()) result = -1;
            if(this.getZ() < t.getZ()) result = 1;
            return result;
        } catch(Exception e) { return result; }
    }
}
